package uk.co.caprica.vlcj.oop.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
 * System properties utilities.
 */
public final class SystemProperties {

    /**
     * Get a list of system properties.
     * <p>
     * This returns a collection of strings in the format:
     * <pre>
     *      -Dsystem.property.name=value
     * </pre>
     * 
     * @param includes names of the properties to include, or <code>null</code> for all properties
     * @return list of system properties
     */
    public static List<String> systemProperties(String... includes) {
        StringBuilder sb = new StringBuilder(40);
        Properties properties = System.getProperties();
        Set<Object> keys = properties.keySet();
        List<String> result = new ArrayList<String>(keys.size());
        for(Object key : properties.keySet()) {
            String name = key.toString();
            if(include(name, includes)) { 
                String value = properties.getProperty(name);
                result.add(sb.append("-D").append(name).append('=').append(value).toString());
                sb.setLength(0);
            }
        }
        return result;
    }

    /**
     * Test whether or not a named system property should be included.
     * 
     * @param name name of the system property
     * @param includes names of the properties to include, or <code>null</code> for all properties
     * @return <code>true</code> if the property should be included; <code>false</code> otherwise
     */
    private static boolean include(String name, String... includes) {
        if(includes != null) {
            for(String include : includes) {
                if(name.equals(include)) {
                    return true;
                }
            }
            return false;
        }
        else {
            return true;
        }
    }
}
